﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Cms.Web.Framework.Controls
{
    [ParseChildren(false)]
    public class AdminTabItem : Control, INamingContainer, ICallbackEventHandler
    {
        private string _enabledImage;
        private HtmlGenericControl _li;
        
        public string Value { get; set; }

        public string Text { get; set; }

        public string EnabledImage
        {
            get { return _enabledImage; }
            set { _enabledImage = value; }
        }

        public string DisabledImage { get; set; }

        public string OnClientClick
        {
            get { return (string)ViewState["OnClientClick"]; }
            set { ViewState["OnClientClick"] = value; }
        }

        #region ICallbackEventHandler Members

        public string GetCallbackResult()
        {
            return string.Empty;
        }
        
        public void RaiseCallbackEvent(string eventArgument)
        {
        }

        #endregion

        protected override void AddParsedSubObject(object obj)
        {
            if (obj != null)
                Controls.Add((Control)obj);
        }
        
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (!(Parent is AdminTabStrip))
                throw new ApplicationException("AdminTabItem must be placed inside an AdminTabStrip.");
            _li = new HtmlGenericControl("li");
            if ((Parent as AdminTabStrip).SelectedValue == Value)
            {
                SetupInnerControls(new Label());
            }
            else
            {
                var lb = (LinkButton)SetupInnerControls(new LinkButton());
                lb.CommandArgument = Value;
                lb.OnClientClick = OnClientClick;
                lb.Click += lb_Click;
            }
            Controls.Add(_li);
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if ((Parent as AdminTabStrip).SelectedValue == Value)
                _li.Attributes.Add("class", "active");
        }
        
        protected void lb_Click(object sender, EventArgs e)
        {
            var tabStrip = Parent as AdminTabStrip;
            tabStrip.SetSelectedValue((sender as LinkButton).CommandArgument);
            tabStrip.RaiseOnChange();
        }

        private WebControl SetupInnerControls(WebControl control)
        {
            control.ID = ID;
            control.CssClass = "link";
            control.Controls.Add(new LiteralControl("<span><img src=\"" + _enabledImage + "\" />"));
            while (Controls.Count > 0)
                control.Controls.Add(Controls[0]);
            control.Controls.Add(new LiteralControl("</span>"));
            _li.Controls.Add(control);
            return control;
        }
    }
}